const config = require('./config')

const { StargateClient } = require('@cosmjs/stargate');

const Logger = require('./logger');

const rpc = process.argv[2];
const num = process.argv[3];
const logfile = process.argv[4];
console.log('rpc, num, logfile', rpc, num, logfile);
const logger = new Logger("press", logfile, logfile, "info");

function idleSleep (time) {
    return new Promise((resolve) => setTimeout(resolve, time));
}


const main= async ()=>{
    let oldTime = new Date().getTime();
    logger.info("oldTime: ", oldTime);

    try {

        while(1){
            const client = await StargateClient.connect(rpc);
            let run_time = new Date().getTime();
            let height = await client.getHeight();
            logger.info("run time: ",run_time, ' height: ', height);

            let i = 0;
            while(i<num) {
                logger.info("run time: ",run_time, ' -- i ======= ', i);
     
                let block = await client.getBlock(height-i)
                logger.info("with client: get block: ",block.id);

                let txHash = "FAB049EACACF7ECBA0E01F60B5828505DA93A70ADD2DCE6241F29C819F1AFF4A";

                let tx = await client.getTx(txHash);
                console.log('tx: ', tx);
                i++;

            }

            await idleSleep(5000)

        }
    }
    catch (e) {
        logger.error("e: ", e);
        let newTime = new Date().getTime();
        logger.info("newTime ", newTime);
        let spendTime = newTime - oldTime;

        logger.info('spendTime: ', spendTime);
        logger.info('spendTime / 1000: ', spendTime/ 1000)

    }


}
main();
